package servlet;

import bean.InboundBean;
import bean.OutboundBean;
import com.alibaba.fastjson.JSON;
import util.JdbcUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class OutboundHuntServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //处理中文乱码问题
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter pw = response.getWriter();
        //获取传进来的
        String seeksupplierout = request.getParameter("seeksupplierout");
        String seekrecordout  =	request.getParameter("seekrecordout");
        //获取模糊查询的数据
        System.out.println("进来了查询界面");
        String sql="select * from supplier_out a join record_out b on a.outid=b.outid where a.name like ? or b.rname like ?";
        try {
            PreparedStatement ps = JdbcUtil.getConnection().prepareStatement(sql);
            ps.setString(1, "%"+seeksupplierout+"%");
            ps.setString(2, "%"+seekrecordout+"%");
            ResultSet set=ps.executeQuery();
            ArrayList<OutboundBean> arr = new ArrayList<OutboundBean>();
            while (set.next()) {
                OutboundBean out = new OutboundBean();
                out.setSoutid(set.getInt(1));
                out.setName(set.getString(2));
                out.setPerson(set.getString(3));
                out.setOutid(set.getInt(4));
                out.setEmail(set.getString(5));
                out.setTel(set.getString(6));
                out.setAddress(set.getString(7));
                out.setWid(set.getInt(9));
                out.setType(set.getString(10));
                out.setRname(set.getString(11));
                out.setPrice(set.getDouble(12));
                out.setNumber(set.getDouble(13));
                out.setTime(set.getString(14));
                arr.add(out);
            }
            System.out.println(arr.toString());
            String json = JSON.toJSONString(arr);
            pw.print(json);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
}
